package com.future.heatwave.network

import com.google.gson.GsonBuilder
import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitManager {
    private val apiServiceStart: ApiService
    private val apiService: ApiService
    private val apiServiceOHI: ApiService
    private val apiServiceLock: ApiService
    private val apiServiceLockApp: ApiService
    private val apiServiceLockApp2: ApiService
    private val apiServiceAD: ApiService

    init {
        val retrofitapp = Retrofit.Builder()
            .baseUrl(ApiUrl.BASE_URL)
            .client(OkHttpManagerApp.getOkHttpClient())
//            .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
//            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 添加 RxJava 3 的适配器

            .build()

        apiServiceLockApp = retrofitapp.create(ApiService::class.java)

        val retrofitappStart = Retrofit.Builder()
            .baseUrl(ApiUrl.BASE_URL)
            .client(OkHttpManagerStart.getOkHttpClient())
//            .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
//            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 添加 RxJava 3 的适配器

            .build()

        apiServiceStart = retrofitappStart.create(ApiService::class.java)

        val retrofitAD = Retrofit.Builder()
            .baseUrl(ApiUrl.BASE_URL)
            .client(OkHttpManagerAd.getOkHttpClient())
//            .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
//            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 添加 RxJava 3 的适配器

            .build()

        apiServiceAD = retrofitAD.create(ApiService::class.java)

        val retrofit = Retrofit.Builder()
            .baseUrl(ApiUrl.BASE_URL)
            .client(OkHttpManager.getOkHttpClient())
//            .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
//            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 添加 RxJava 3 的适配器

            .build()

        apiService = retrofit.create(ApiService::class.java)

        val retrofitOHI = Retrofit.Builder()
            .baseUrl(ApiUrl.BASE_URL)
            .client(OkHttpManagerOfficialHeaderInfor.getOkHttpClient())
//            .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
//            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 添加 RxJava 3 的适配器

            .build()

        apiServiceOHI = retrofitOHI.create(ApiService::class.java)

        val retrofitLock = Retrofit.Builder()
            .baseUrl(ApiUrl.BASE_URL)
            .client(OkHttpManagerLock.getOkHttpClient())
//            .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
//            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 添加 RxJava 3 的适配器

            .build()

        apiServiceLock = retrofitLock.create(ApiService::class.java)

        val retrofitLockFree = Retrofit.Builder()
            .baseUrl(ApiUrl.BASE_URL)
            .client(OkHttpManagerLockFreeList.getOkHttpClient())
//            .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
            .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
//            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create())

            .build()

        apiServiceLockApp2 = retrofitLockFree.create(ApiService::class.java)
    }

    fun getApiClient(): ApiService {
        return apiService
    }
    fun getApiClientStart(): ApiService {
        return apiServiceStart
    }
    fun getApiClientAD(): ApiService {
        return apiServiceAD
    }

    fun getApiClientApp(): ApiService {
        return apiServiceLockApp
    }
    fun getApiClientApp2(): ApiService {
        return apiServiceLockApp2
    }
    fun getApiClientLock(): ApiService {
        return apiServiceLock
    }
    fun getApiClientOHI(): ApiService {
        return apiServiceOHI
    }
    fun cancelAllRequests() {
        val dispatcher = OkHttpManager.getOkHttpClient().dispatcher
        // 取消所有请求
        dispatcher.cancelAll()
    }
}